﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections;
using JEng_Mark_1.Interfaces;
using JEng_Mark_1.Textures;
using System.Threading;

namespace JEng_Mark_1.Managers
{
    public class JTextureManager : GameComponent
    {
        private static Dictionary<string, IJEngTexture> _textures = new Dictionary<string, IJEngTexture>();

        private static bool _initialized = false;

        /// <summary>
        /// Is the TextureManager initialized
        /// </summary>
        public static bool Initialized { get { return _initialized; } }

        private static int _texturesLoaded = 0;

        /// <summary>
        /// The number of textures that are currently loaded.
        /// </summary>
        public static int TexturesLoaded { get { return _texturesLoaded; } }

        public JTextureManager(Game game)
            : base(game)
        { }

        /// <summary>
        /// Add a texture to the collection of loaded textures
        /// </summary>
        /// <param name="newTexture">Texture to add</param>
        /// <param name="textureName">Name of texture for later referencing</param>
        public static void AddTexture(IJEngTexture newTexture, string textureName)
        {
            if (textureName != null && !_textures.ContainsKey(textureName))
            {
                _textures.Add(textureName, newTexture);
                
                if (_initialized)
                {
                    ThreadStart threadStarter = delegate
                    {
                        newTexture.LoadContent();
                        _texturesLoaded++;
                    };
                    Thread loadingThread = new Thread(threadStarter);
                    loadingThread.Start();
                }
            }
        }

        /// <summary>
        /// Remove a texture and destroy the asset by reference
        /// </summary>
        /// <param name="textureName">Name of texture to destroy</param>
        public static void RemoveTexture(string textureName)
        {
            if (textureName != null && _textures.ContainsKey(textureName))
            {
                if (_initialized)
                {
                    ThreadStart threadStarter = delegate
                    {
                        _textures[textureName].UnloadContent();
                        _textures.Remove(textureName);
                        _texturesLoaded--;
                    };
                    Thread loadingThread = new Thread(threadStarter);
                    loadingThread.Start();
                }
            }
        }

        /// <summary>
        /// Get a texture by name
        /// </summary>
        /// <param name="textureName">Name of texture to retrieve</param>
        /// <returns>The texture requrested, or if the texture is not found null</returns>
        public static IJEngTexture GetTexture(string textureName)
        {
            if (textureName != null && _textures.ContainsKey(textureName))
            {
                return _textures[textureName];
            }
            return null;
        }

        /// <summary>
        /// Initialize the manager and load all textures already added
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();

            ThreadStart threadStarter = delegate
            {
                foreach (IJEngTexture texture in _textures.Values)
                {
                    if (!texture.ReadyToRender)
                    {
                        texture.LoadContent();
                        _texturesLoaded++;
                    }
                }
            };
            Thread loadingThread = new Thread(threadStarter);
            loadingThread.Start();

            _initialized = true;
        }
    }
}
